
package com.ifcar.app.activity.news;

import com.ifcar.app.activity.CommonFrameActivity;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.fragment.NRLoadingListFrament;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.BasePageViewAdapter;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.Tools;
import com.ifcar.app.activity.R;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsMoreListFragment extends NRLoadingListFrament {
    private BasePageViewAdapter mAdapter;

    private List<Map<String, Object>> mData;

    private boolean mUpdateData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.load_list_layout, container, false);
    }

    @Override
    public void onViewCreatedNR(View view, Bundle savedInstanceState) {
        super.onViewCreatedNR(view, savedInstanceState);
        getListView().setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String tid = null;
                String tname = null;
                try {
                    tid = StringUtil.GetString(((HashMap<String, Object>) mData
                            .get(position)).get(GetColumnsInfo.TID_TAG));
                    tname = StringUtil.GetString(((HashMap<String, Object>) mData
                            .get(position)).get(GetColumnsInfo.TNAME_TAG));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                if (TextUtils.isEmpty(tname) || TextUtils.isEmpty(tid)) {
                    return;
                }
                
                Bundle b = new Bundle();
                b.putString(NewsListFragment.COLUMN_ID, tid);
                b.putString(NewsListFragment.COLUMN_URL, tid);
                b.putString(NewsListFragment.COLUMN_NAME, tname);
                
                Intent intent = CommonFrameActivity.getStartIntent(getActivity(), tname,
                        NewsListViewFragment.class, tid, b);

                startActivity(intent);

            }
        });

        Button more = (Button) getMoreButton();
        more.setText(R.string.custom_title);
        more.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              
            }
        });
        
        callLoadingListViewAddMoreView();
    }

    @Override
	public ListAdapter getAdapter() {
		return mAdapter;
	}

	@Override
    protected boolean autoRefreshOnFirst() {
        return false;
    }

    @Override
    public boolean isNeedRefresh() {
        return false;
    }

    @Override
    public void onTabSelected(boolean first) {
        super.onTabSelected(first);
        if (first || mUpdateData) {
            mUpdateData = false;
            startLoading();
        }
    }

    @Override
    protected void onLoading() {
        super.onLoading();

        List<Map<String, Object>> list = getMoreList();
        Message msg = getHandler().obtainMessage(MSG_LOADING);
        msg.obj = list;
        getHandler().dispatchMessage(msg);
    }

    @Override
    protected void onHandleLoading(Object result) {
        super.onHandleLoading(result);
        List<Map<String, Object>> list = (List<Map<String, Object>>) result;
        setData(list);
    }

    private List<Map<String, Object>> getMoreList() {
        List<Map<String, Object>> moreData = new ArrayList<Map<String, Object>>();

        Cursor cursor = Tools.getDataFromDB(getActivity(), NewsSQLiteOpenHelper.MORE_COLUMN_TABLE,
                getDb());
        if (cursor == null) {
            return moreData;
        }

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(GetColumnsInfo.TNAME_TAG, cursor.getString(cursor
                    .getColumnIndex(NewsSQLiteOpenHelper.TNAME)));
            map.put(GetColumnsInfo.TID_TAG, cursor.getString(cursor
                    .getColumnIndex(NewsSQLiteOpenHelper.TID)));
            map.put(GetColumnsInfo.CID_TAG, cursor.getString(cursor
                    .getColumnIndex(NewsSQLiteOpenHelper.CID)));
            moreData.add(map);
        }

        cursor.close();

        return moreData;
    }

    private void setData(List<Map<String, Object>> topicData) {
        mData = topicData;
        if (topicData != null) {
            mAdapter = new BasePageViewAdapter(getActivity(), mData, R.layout.more_list_item,
                    new String[] {
                        GetColumnsInfo.TNAME_TAG
                    }, new int[] {
                        R.id.column_title
                    }, null);

        } else {
            mAdapter = null;
        }
        getListView().setAdapter(mAdapter);
    }

    public void updateData() {
        mUpdateData = true;
    }

}
